package com.example.recruit.common;

import com.example.recruit.fiter.CheckLoginFilter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.annotation.WebFilter;
import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;

/**
 * 全局异常捕获
 */
@ControllerAdvice(annotations = {RestController.class, Controller.class})

@ResponseBody
@Slf4j
public class GlobalExceptionHandler {
    /**
     * AOP切面
     */
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R<String> exceptionHandler(SQLIntegrityConstraintViolationException e){
        log.error(e.getMessage());
        if(e.getMessage().contains("Duplicate entry ")){
            String[] s=e.getMessage().split(" ");
            String message=s[2]+"已存在";
            return R.error(message);
        }
        return R.error("未知错误");

    }
    @ExceptionHandler(SQLException.class)
    public R<String> SQLexception (SQLException e){
        log.info(e.getMessage());
        return R.error("未知错误");
    }

    @ExceptionHandler(IllegalStateException.class )
    public R<String> IllegalStateException(IllegalStateException e){
        log.info(e.getMessage());
        return R.error("非法参数");
    }

    @ExceptionHandler(JurisdictionException.class)
    public R<String> jurisdictionHander(){
        log.info("权限");
        return R.error("权限不足");
    }
    /**
     * 异常处理方法
     * @param ex
     * @return
     */
    @ExceptionHandler(CustomException.class)
    public R<String > exceptionHandler(CustomException ex){
        log.error(ex.getMessage());
        return R.error(ex.getMessage());
    }
}
